<?php
namespace app\admin\controller;

use app\admin\model\Member as MemberModel;
use Request;

/**
 * 用户分信息
 */
class Member extends Base
{
    /*
    显示分类信息列表
     */
    public function index($value = '')
    {
        $list = MemberModel::where('tid', 2)->paginate(20);
        $this->assign([
            'list' => $list,
            'page' => $list->render(),
        ]);
        //echo request()->action();
        return $this->fetch();
    }

    public function listjson($value = '')
    {
        $list = MemberModel::where('tid', 2)->paginate(20);
        $this->assign([
            'list' => $list,
            'page' => $list->render(),
        ]);
        return json_encode($list);
        //echo request()->action();
        //return $this->fetch();
    }


    /*
    信息添加
     */
    public function add()
    {
        if ($this->request->isPost()) {

            $post = $this->request->post();

            $result = $this->validate($post, 'app\admin\validate\User.add');
            if (true !== $result) {
                return $this->error($result);
            }

            $data = MemberModel::create($post);
            if (!empty($data)) {
                return $this->success('添加成功', 'admin/Member/index');
            }
        }
        // $data = AuthGroupModel::where(['status' => 1])->select();
        // $this->assign('tname', $data);
        return $this->fetch();
    }

    /*
    信息编辑
     */
    public function edit()
    {
        $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
        if ($this->request->isPost()) {

            $post = $this->request->post();

            $result = $this->validate($post, 'app\admin\validate\User.edit');
            if (true !== $result) {
                return $this->error($result);
            }
            $model = MemberModel::edit($post, $id);

            if ($model || $astauts) {
                return $this->success('修改成功', 'admin/Member/index');
            } else {
                return $this->error('修改失败');
            }
        }

        $info = MemberModel::get($id);
        if (empty($info)) {return $this->error('id参数错误');}

        // $data  = AuthGroupModel::where(['status' => 1])->select();
        // $ninfo = Db::name('auth_group_access')->where('uid', $id)->select(); //当前拥有的

        // foreach ($data as $k => $v) {
        //     foreach ($ninfo as $k2 => $v2) {
        //         if ($v['id'] == $v2['group_id']) {
        //             $data[$k]['checked'] = 'y';
        //         }
        //     }
        // }
        $this->assign([
            //'tname' => $data,
            'info' => $info,
        ]);
        return $this->fetch();
    }

    /*
    信息删去
     */
    public function delete($value = '')
    {
        $id   = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
        $data = MemberModel::get($id); // 获取要删除的对象  不存在时返回 null    $user::destroy($id) //批量删时尽量用，返回记录条数，没有删除返回0
        if (!empty($data)) {
            if ($data->delete()) {
                return $this->success('删去成功', 'admin/Member/index', '', 1);
            }
        }
        return $this->error('删去失败');
    }

    /**
     * 快捷操作
     * @return [type] [description]
     */
    public function quickedit()
    {
        $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
        if ($this->request->isPost()) {
            if (input('?post.remark')) {
                $data = ['id' => Request::post('id/d'), 'remark' => Request::post('remark')]; //备注
            } else if (input('?post.status')) {
                $data = ['id' => Request::post('id/d'), 'status' => Request::post('status')]; //状态
            }
            $model = MemberModel::update($data);
            if (!empty($model)) {
                return $this->success('修改成功');
            } else {
                return $this->error('修改失败');
            }
        }
    }

}
